﻿using CoreHelper;
using CoreWeb.Serialization;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace CoreWeb.Authentication
{
    public class LoginUserManager
    {

        private Microsoft.AspNetCore.Http.HttpContext httpContext;
        public LoginUserManager(IHttpContextAccessor httpContextAccessor)
        {
            httpContext = httpContextAccessor.HttpContext;
        }

        /// <summary>
        /// 默认加密key
        /// </summary>
        public const string DefaultEncryptKey = "?a_b.#()";
        /// <summary>
        /// 默认token名称
        /// </summary>
        public const string DefaultTokenKey = "apiToken";

        /// <summary>
        /// 从token获取到用户信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public LoginUser GetUser(string key, string encryptKey = DefaultEncryptKey)
        {
            return GetUser<LoginUser>(key, encryptKey);
        }

        /// <summary>
        /// 从token获取到用户信息
        /// </summary>
        /// <typeparam name="TUser"></typeparam>
        /// <param name="key"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public TUser GetUser<TUser>(string key, string encryptKey = DefaultEncryptKey) where TUser : LoginUser
        {
            if (!httpContext.Items.ContainsKey(key))
            {

                var token = httpContext.Request.Query[key];
                if (string.IsNullOrEmpty(token))
                {
                    token = httpContext.Request.Form[key];
                }
                if (string.IsNullOrEmpty(token))
                {
                    token = httpContext.Request.Headers[key];
                }
                var user = ResolveUser<TUser>(token, encryptKey);
                httpContext.Items[key] = user;
                return user;
            }
            else
            {
                return httpContext.Items[key] as TUser;
            }
        }

        /// <summary>
        /// 从给定token里重建用户
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static TUser ResolveUser<TUser>(string token, string encryptKey = DefaultEncryptKey) where TUser : LoginUser
        {
            if (token != null && token.Length > 0)
            {
                token = HttpUtility.UrlDecode(token);
                var json = EncryptHelper.DecryptString(token, encryptKey);
                var loginuser = JsonHelper.DeserializeFromString<TUser>(json);
                if (loginuser != null)
                {
                    if (DateTime.Parse(loginuser.Expires) >= DateTime.Now)
                    {
                        return loginuser;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 获取用户的加密token
        /// </summary>
        /// <param name="user"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public static string GetUserToken(LoginUser user, string encryptKey = DefaultEncryptKey)
        {
            return GetUserToken<LoginUser>(user, encryptKey);
        }

        /// <summary>
        /// 获取用户的加密token
        /// </summary>
        /// <typeparam name="TUser"></typeparam>
        /// <param name="user"></param>
        /// <param name="encryptKey"></param>
        /// <returns></returns>
        public static string GetUserToken<TUser>(TUser user, string encryptKey = DefaultEncryptKey) where TUser : LoginUser
        {
            var json = JsonHelper.SerializeToString(user);
            var value = EncryptHelper.EncryptString(json, encryptKey);
            return HttpUtility.UrlEncode(value);
        }
    }
}
